package com.zyylr.classroomreservationsystem.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.zyylr.classroomreservationsystem.R;
import com.zyylr.classroomreservationsystem.activity.AboutActivity;
import com.zyylr.classroomreservationsystem.activity.ChangePasswordActivity;
import com.zyylr.classroomreservationsystem.bean.LoginUser;
import com.zyylr.classroomreservationsystem.bean.User;
import com.zyylr.classroomreservationsystem.consts.GlobalConsts;
import com.zyylr.classroomreservationsystem.others.ListViewUserCenterAdapter;
import com.zyylr.classroomreservationsystem.activity.MyReservationActivity;
import com.zyylr.classroomreservationsystem.bean.Information;

import java.util.ArrayList;
import java.util.List;

public class UserCenterFragment extends Fragment {
    private List<Information> informationList = new ArrayList<Information>();
    private Button btnLogout;
    private TextView tvUserName;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_user_center, container, false);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        btnLogout = getActivity().findViewById(R.id.btn_logout);
        tvUserName = getActivity().findViewById(R.id.tv_username);

        initInformation();

        ListViewUserCenterAdapter adapter = new ListViewUserCenterAdapter(getActivity(), R.layout.listview_item, informationList);
        ListView listView = (ListView) getActivity().findViewById(R.id.list_view);

        listView.setAdapter(adapter);

        //点击每个listview中item进行跳转
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                /**
                 * 普通用户，有三个item：
                 *      0.我的预约
                 *      1.密码修改
                 *      2.关于软件
                 */
                if (LoginUser.getInstance().getRole() == User.ROLE_COMMON) {
                    switch (i) {
                        case 0:
                            startActivity(new Intent(getActivity(), MyReservationActivity.class));
                            break;
                        case 1:
                            startActivity(new Intent(getActivity(), ChangePasswordActivity.class));
                            break;
                        case 2:
                            startActivity(new Intent(getActivity(), AboutActivity.class));
                            break;
                    }
                }
                /**
                 * 管理员只有两个item：
                 *      0.密码修改
                 *      1.关于软件
                 */
                else {
                    switch (i) {
                        case 0:
                            startActivity(new Intent(getActivity(), ChangePasswordActivity.class));
                            break;
                        case 1:
                            startActivity(new Intent(getActivity(), AboutActivity.class));
                            break;
                    }
                }
            }
        });

        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoginUser.logout();
                Intent intent = new Intent(GlobalConsts.BROADCAST_LOGOUT);
                getActivity().sendBroadcast(intent);
            }
        });
        tvUserName.setText(LoginUser.getInstance().getName());
    }

    /**
     * 初始化信息（listView的item数据）
     */
    private void initInformation() {
        if (LoginUser.getInstance().getRole() == User.ROLE_COMMON) {
            Information myReser = new Information("我的预约", R.drawable.wodeyuyue, R.drawable.right_arrow);
            informationList.add(myReser);
        }
        Information editPas = new Information("密码修改", R.drawable.mimaxiugai1, R.drawable.right_arrow);
        informationList.add(editPas);
        Information about = new Information("关于软件", R.drawable.about, R.drawable.right_arrow);
        informationList.add(about);

    }


}
